/**
 * Created by KLSD on 2017/9/26.
 */

//保存添加修改
function saveCategory() {
    if ($('#cateName').val().trim().length == 0) {
        alert("分类名不能为空！");
    } else if (/^[\u4E00-\u9FA5A-Za-z0-9]{1,16}$/.test($("#cateName").val())) {
        if ($('#cateDescription').val().trim().length == 0) {
            alert("分类描述不能为空！");
        } else if (/^[\u4E00-\u9FA5A-Za-z0-9]{1,16}$/.test($("#cateDescription").val())) {
            var data = {
                cateId: $('#cateId').val(),
                cateName: $('#cateName').val(),
                cateDescription: $('#cateDescription').val(),
                groupId: $('#groupId').val(),
                sort: $('#sort').val()
            }
            $.ajax({
                type: "post",
                dataType: "json",
                url: "/view/updateCategory",
                data: data,
                success: function (data) {
                    window.location.href = "/view/category?pageNumber=1&pageSize=10";
                },
            });
        } else {
            alert("保存失败！分类描述由1-16中文,英文，数字组成!!");
        }
    } else {
        alert("保存失败！分类名由1-16中文,英文，数字组成!!");
    }
}

function cancel() {
    window.location = "/view/category?pageNumber=1&pageSize=10";
}

function searchCategory() {
    var data = {
        cateName: $('#cateName').val(),
        isShow: $('#isShow').val(),
        groupId: $('#groupId').val(),
        rankType: $('#rankType').val(),
        orderType: $('#orderType').val()
    }
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/view/searchCategory?pageNumber=1&pageSize=10",
        data: data,
        success: function (data) {
            var html = "";

            if (null != data && data.data.cateList.list.length > 0) {
                for (var i = 0; i < data.data.cateList.list.length; i++) {
                    var o = data.data.cateList.list[i];
                    html += "  <tr><td><span class=\"uni\"><input type='checkbox' value='check1'/></span> </td>"
                        + "<td>" + o.sort + "</td>"
                        + "<td>" + o.cateName + "</td>"
                        + "<td>" + o.cateDescription + "</td>"
                        + "<td>" + o.groupName + "</td>"
                        + "<td>" + o.args1 + "</td>"
                        + "<td>" + o.createTime + "</td>"
                        + "<td>编辑</td>";
                }
            }
            $("tbody").empty().append(html);

            var d = data.data.cateList;
            var temp = '<div id="page3"><input type="hidden" id="pageNumber" value="' + d.pageNum + '">' +
                '<input type="hidden" id="pageSize" value="' + d.pageSize + '">' +
                '<input type="hidden" id="totalPage" value="' + d.pages + '">' +
                '<input type="hidden" id="total" value="' + d.total + '"></div>'
           $(".widget-foot").empty().html(temp);
            initPage('page3', '/view/searchCategory', {}, false);
        }
    });


}

